var app = getApp();
var pageIndex = 1;
var step=10;
var pageSize = step;
var hasNext;
var arr = [];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    select:'全部'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      select:options.select
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    pageIndex = 1;
    hasNext=false;
    arr = [];
    this.getInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(hasNext){
      pageIndex += 1;
      this.getInfo()
    }else{
      wx.showToast({
        title: '暂无更多数据',
        icon: 'none',
        duration: 2000
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  select(e){
    this.setData({
      select:e.currentTarget.dataset.info
    })
    pageIndex = 1;
    arr = [];
    this.getInfo();
  },
  getInfo(){
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    wx.request({
      url: app.getUrl() + '/api/order_info/query',
      data:{
        orderStatus:this.data.select,
        Authorization: app.globalData.customerId + ":" + app.globalData.accessToken,
        pageIndex:pageIndex,
        pageSize:pageSize,
      },
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      success(res) {
        if (res && res.statusCode == 200 && res.data && res.data.status == 200) {
          wx.hideLoading({
            success: (res) => {},
          })
          hasNext = res.data.obj.hasNext;
          arr = arr.concat(res.data.obj.rows);
          if(res.data.obj.total == 0 ){
            wx.showToast({
              title: '暂无对应订单',
              icon: 'none',
              duration: 2000
            })
          }
          console.log(arr)
          that.setData({
            list:arr,
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail(res) {
        console.log(res)
        wx.showToast({
          title: '连接服务器失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  pay(e){
    console.log(e.currentTarget.dataset)
    if(e.currentTarget.dataset.status=='待支付'){
      wx.showLoading({
        title: '请稍后',
        mask:true
      })
      wx.login({
        success(res){
          if (res.code){
            //小程序预支付
            wx.request({
              url: app.getUrl() + '/api/order_info/wexcx_prepare_pay',
              data:{
                orderId: e.currentTarget.dataset.orderid,
                code:res.code,
                // Authorization: app.globalData.customerId + ":" + app.globalData.accessToken,
              },
              method: 'POST',
              header: {
                "Content-Type": "application/x-www-form-urlencoded",
              },
              success(res) {
                wx.hideLoading({
                  success: (res) => {},
                })
                if (res && res.statusCode == 200 && res.data && res.data.status == 200) {
                  wx.requestPayment({
                    timeStamp: res.data.obj.timeStamp,
                    nonceStr: res.data.obj.nonceStr,
                    package: res.data.obj.wepaypackage,
                    signType: res.data.obj.signType,
                    paySign: res.data.obj.paySign,
                    success: function (res) {
                      if (res.errMsg == "requestPayment:ok") {
                        //获取支付信息
                        wx.showToast({
                          title: '支付成功',
                          icon: 'success',
                          duration: 2000
                        })
                        pageIndex = 1;
                        hasNext=false;
                        arr = [];
                        that.setData({
                          select:"全部"
                        })
                        that.getInfo()
                      }else{
                        console.log(res)
                        wx.showToast({
                          title: '支付失败',
                          icon: 'none',
                          duration: 2000
                        })
                      }
                    },
                    fail: function (res) {
                      console.log(res)
                      wx.showToast({
                        title: '支付失败',
                        icon: 'none',
                        duration: 2000
                      })
                    },
                  })
                }else{
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 2000
                  })
                }
              },
              fail(res) {
                console.log(res)
                wx.showToast({
                  title: '连接服务器失败',
                  icon: 'none',
                  duration: 2000
                })
              }
            })
          }else{
            wx.showToast({
              title: '获取用户登录态失败',
              icon: 'none',
              duration: 2000
            })
            console.log('获取用户登录态失败！' + res.errMsg)
          }
        },
        fail: function (res) {
          wx.showToast({
            title: '微信登录失效，请重新登录',
            icon: 'none',
            duration: 2000
          })
        }
      })
    }
    
  },
  cancel(e){
    var that=this;
    //调用取消订单接口
    wx.request({
      url: app.getUrl() + '/api/order_info/cancel_order',
      data:{
        orderId:e.currentTarget.dataset.orderid,
        Authorization: app.globalData.customerId + ":" + app.globalData.accessToken,
      },
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      success(res) {
        console.log(res)
        if (res && res.statusCode == 200 && res.data && res.data.status == 200) {
          wx.showToast({
            title: '取消成功',
            icon: 'success',
            duration: 2000
          })
          pageIndex = 1;
          hasNext=false;
          arr = [];
          that.setData({
            select:"全部"
          })
          that.getInfo()
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail(res) {
        console.log(res)
        wx.showToast({
          title: '连接服务器失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  }
})